package com.game.screen.level;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.geom.AffineTransform;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;

import com.game.screen.Player;
import com.game.util.ProcessingObject;

import processing.core.PApplet;
import processing.core.PImage;

public class LevelLoader extends ProcessingObject{

    private ArrayList<PImage> obstacles = new ArrayList<PImage>();
    private int currentMap;

    public Map<String, PImage> imgs = new HashMap<String, PImage>();
    
    
    public LevelLoader() {
    	loadObstacleImages();
    }

	public void loadObstacleImages() {
		imgs.put("wall", pApplet.loadImage("obstacle1.png"));
		imgs.put("blueprize", pApplet.loadImage("blueLego.png"));
		imgs.put("redprize", pApplet.loadImage("redLego.png"));
		imgs.put("greenprize", pApplet.loadImage("greenLego.png"));
		imgs.put("yellowprize", pApplet.loadImage("yellowLego.png"));
		imgs.put("easteregg1", pApplet.loadImage("obstacle1.png"));
		imgs.put("easteregg2", pApplet.loadImage("obstacle1.png"));

		imgs.put("blub", pApplet.loadImage("lightbulb.png"));
    }
	
	public LevelMap loadMap(String filename)  throws IOException {
	    ArrayList lines = new ArrayList();
	    int width = 5;
	    int height = 10;
	
	    // read every line in the text file into the list
//	    BufferedReader reader = new BufferedReader(
//	        new FileReader(filename));
	    
	    InputStream instream = getClass().getResourceAsStream(filename);
	    InputStreamReader infile = new InputStreamReader(instream);
	    BufferedReader reader = new BufferedReader(infile);

	    
	    while (true) {
	        String line = reader.readLine();
	        // no more lines to read
	        if (line == null) {
	            reader.close();
	            break;
	        }
	
	        // add every line except for comments
	        if (!line.startsWith("#")) {
	            lines.add(line);
//	            width = Math.max(width, line.length());
	        }
	    }

	    // parse the lines to create a TileEngine
	    height = lines.size();
	    LevelMap newMap = new LevelMap(width, height);
	    for (int y=0; y<height; y++) {
	        String line = (String)lines.get(y);
	        for (int x=0; x<line.length(); x++) {
	            char ch = line.charAt(x);
	            
	            // check if the char represents tile A, B, C etc.

	            int posX = (int)(x * 30);
	            int posY = (int)((y - height) * 30) + 90;
	
//	            System.err.println(ch + ":" + posX + ":" + posY);
	            
	            int tile = ch - 'A';
	           if (tile >= 0 && tile < obstacles.size()) {
	                //newMap.setTile(posX, posY, (PImage)obstacles.get(tile));
	           }
	
	            // check if the char represents an obstacle
	            else if (ch == '@') {
	                newMap.addObstacle(new Wall(imgs.get("wall"), posX, posY));
	            }
	            else if (ch == 'B') {
	            	newMap.addObstacle(new Prize(imgs.get("blueprize"), posX, posY, Player.BLUE));
	            }
	            else if (ch == 'Y') {
	            	newMap.addObstacle(new Prize(imgs.get("yellowprize"), posX, posY, Player.YELLOW));	            
	            }
	            else if (ch == 'G') {
	            	newMap.addObstacle(new Prize(imgs.get("greenprize"), posX, posY, Player.GREEN));	            
	            }
	            else if (ch == 'R') {
	            	newMap.addObstacle(new Prize(imgs.get("redprize"), posX, posY, Player.RED));	            
	            }
	            else if (ch == '!') {
	            	newMap.addObstacle(new Prize(imgs.get("easteregg1"), posX, posY, Player.EEGG1));	            
	            }
	            else if (ch == 'X') {
	            	newMap.addObstacle(new Prize(imgs.get("easteregg2"), posX, posY, Player.EEGG2));	            
	            }
	            else if (ch == '+') {
	            	newMap.addObstacle(new Goal(imgs.get("blub"), posX, posY, Player.EEGG2));	            
	            }
	        }
	    }

	    return newMap;
	}

    
}
